<template>
  <div class="tree-item">
    <span class="i1" style="font-weight: 700">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <el-dropdown @command="commandEvent">
      <span
        ><span>操作</span> <i class="el-icon-arrow-down el-icon--right"></i
      ></span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="look"
            >查看部门</el-dropdown-item
          >
          <el-dropdown-item v-if="!item.isTop" command="del"
            >删除部门</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
// 导入删除接口
import { companyDepartmentDel } from '@/api/departments.js'
export default {
  // 接收data数据
  props: ['item'],
  methods: {
    commandEvent (command) {
      switch (command) {
        // 添加部门
        case 'add':
          // 触发兄弟组件的show方法,并将true,和当前点击项的数据传过去
          this.$bus.$emit('show', true, this.item, 'add')
          break
        // 删除部门
        case 'del':
          this.$confirm('确定删除此部门吗?', '提示').then(async () => {
            // 将当前点击项的id传递过去
            await companyDepartmentDel(this.item.id)
            // 提示
            this.$message.success('删除成功')
            // 刷新
            this.$emit('getDate')
          })
          break
        // 编辑部门
        case 'look':
          // 打开弹窗
          this.$bus.$emit('show', true, this.item, 'edit')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  display: flex;
  align-items: center;
  .i1 {
    flex: 1;
    width: 100px;
  }
  .i2 {
    width: 100px;
  }
}
</style>
